<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

class ShopModelHome extends JModelItem
{

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * return	void
	 * @since	1.6
	 */
	protected function populateState($ordering = null, $direction = null)
	{
		// Initiliase variables.
		$app	= JFactory::getApplication('site');
	
		// Load the parameters. Merge Global and Menu Item params into new object
		$params = $app->getParams();
		$menuParams = new JRegistry;

		if ($menu = $app->getMenu()->getActive()) {
			$menuParams->loadString($menu->params);
		}

		$mergedParams = clone $menuParams;
		$mergedParams->merge($params);

		$this->setState('params', $mergedParams);
		$user		= JFactory::getUser();
		// Create a new query object.
		$this->setState('product.isnew',$params->get('isnew',0));
		$this->setState('product.newtitle',$params->get('newtitle','Sản phẩm mới'));
		$this->setState('product.issale',$params->get('issale',0));
		$this->setState('product.saletitle',$params->get('saletitle','Sản phẩm khuyến mãi'));
		$this->setState('product.islot',$params->get('islot',0));
		$this->setState('product.lottitle',$params->get('lottitle','Sản phẩm bán chạy'));
		$this->setState('product.widthimg',$params->get('widthimg',150));
		$this->setState('product.heightimg',$params->get('heightimg',150));
		$this->setState('product.price',$params->get('price',1));
		$this->setState('product.oldprice',$params->get('oldprice',1));
		$this->setState('product.button',$params->get('button',1));
		$this->setState('product.limit',$params->get('limit',3));
		$this->setState('page.columns',$params->get('columns',3));
		$this->setState('page.show_page_heading',$params->get('show_page_heading',0));
		$this->setState('page.page_title',$params->get('page_title','No Title'));
	}
	// Get new product
	public function getNews()
	{
		//$params 	= $this->getState()->get('params');
		$limit 		= $this->getState('product.limit');
		
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$query->clear();
		$query->select('*');
		$query->from('#__shop_product');
		$query->where('state = 1');
		if($this->getState('product.isnew') == 1) {
			$query->where('isnew = 1');
		}
		$query->order('id DESC');
		$db->setQuery((string)$query, 0, $limit);
		$news = $db->loadObjectList();
		return $news;
	}
	// Get sale product
	public function getSales()
	{
		//$params 	= $this->getState()->get('params');
		$limit 		= $this->getState('product.limit');
		
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$query->clear();
		$query->select('*');
		$query->from('#__shop_product');
		$query->where('state = 1');
		if($this->getState('product.issale') == 1) {
			$query->where('issale = 1');
		}
		$query->order('id DESC');
		$db->setQuery((string)$query, 0, $limit);
		$sales = $db->loadObjectList();
		
		return $sales;
	}
	// Get lot product
	public function getLots()
	{
		//$params 	= $this->getState()->get('params');
		$limit 		= $this->getState('product.limit');
		
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$query->clear();
		$query->select('*');
		$query->from('#__shop_product');
		$query->where('state = 1');
		if($this->getState('product.islot') == 1) {
			$query->where('islot = 1');
		}
		$query->order('id DESC');
		$db->setQuery((string)$query, 0, $limit);
		$lots = $db->loadObjectList();
		
		return $lots;
	}
}